export default class IconKObj extends godot.KinematicBody2D {
    // 移动速度
    v = new godot.Vector2()

    constructor(x = 200, y = 200, img = "") {
        super()
        // Area2D  碰撞对象体
        this.position = new godot.Vector2(x, y)

        // Sprite  外观
        var spr = new godot.Sprite()
        if (img) {
            spr.texture = godot.load(img) as godot.Texture
        } else {
            spr.texture = godot.load("res://assets/coin.png") as godot.Texture
        }

        this.add_child(spr);

        // Shape   碰撞盒
        let shape = new godot.RectangleShape2D()
        let w = spr.texture.get_width()
        let h = spr.texture.get_height();
        shape.extents = new godot.Vector2(w / 2, h / 2)

        let oid = this.create_shape_owner(this)
        this.shape_owner_add_shape(oid, shape)

        // KObj无法响应Area2D信号
    }

    _physics_process(delta: number): void {
        this.handleInput();

        // 在Kobj处理与Obj的碰撞信息
        let res = this.move_and_collide(this.v)
        if(res){
            console.log(new Date(),"碰撞到")
        }
    }

    handleInput(){
        this.v = new godot.Vector2()
        let d = 3
        if(godot.Input.is_key_pressed(godot.KEY_D)){
            this.v.x = d
        }
        if(godot.Input.is_key_pressed(godot.KEY_A)){
            this.v.x = -d
        }   
        if(godot.Input.is_key_pressed(godot.KEY_W)){
            this.v.y = -d
        }
        if(godot.Input.is_key_pressed(godot.KEY_S)){
            this.v.y = d
        }   
    }
}